package com.xuxueli.applyModules.controller;
import com.xuxueli.applyModules.entity.ServiceGroupDictionaryInfo;
import com.xuxueli.applyModules.entity.ServiceGroupInfo;
import com.xuxueli.applyModules.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/testEleasticSearch")
@Slf4j
public class TestEleasticSearch {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @GetMapping("/getvalue/{content}")
    public void getvalue(@PathVariable("content") String content,
                         @RequestParam(value = "visible",required = false) Integer visible,
                         @RequestParam(value = "ids",required = false) String ids){
        //从存储引擎取值
        GetQuery getRequest = new GetQuery();
        getRequest.setId("1");
        Users user = elasticsearchRestTemplate.queryForObject(getRequest, Users.class);
        //第一种，测试成功
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        TermQueryBuilder statusQueryBuilder = QueryBuilders.termQuery("visible", 1);
        boolQueryBuilder.must(statusQueryBuilder);
        //must:   AND
        //mustNot: NOT
        //should:: OR
        if (StringUtils.isNotBlank(content)) {
            BoolQueryBuilder contentQueryBuilder = QueryBuilders.boolQuery();
            WildcardQueryBuilder groupNameWildcardQueryBuilder = QueryBuilders
                    .wildcardQuery("name", "*" + content.toLowerCase() + "*");
            MatchQueryBuilder groupNameQueryBuilder = new MatchQueryBuilder("name", content);
            MatchQueryBuilder briefMatchQueryBuilder = new MatchQueryBuilder("code", content);
            contentQueryBuilder
                    .should(groupNameWildcardQueryBuilder)
                    .should(groupNameQueryBuilder)
                    .should(briefMatchQueryBuilder);
            boolQueryBuilder.must(contentQueryBuilder);
        }
        queryBuilder.withQuery(boolQueryBuilder);
        queryBuilder.withFilter(QueryBuilders.rangeQuery("time.keyword")
                .from("2021-12-01")
                .to("2021-12-08")
                .includeLower(true)     //包括下界
                .includeUpper(false));  //包括上界
        queryBuilder.withQuery(QueryBuilders.termQuery("visible", 1));
        queryBuilder.withSort(new FieldSortBuilder("time.keyword").order(SortOrder.DESC));
        queryBuilder.withPageable(PageRequest.of(0, 10000));
        List<Users> userList = elasticsearchRestTemplate.queryForList(queryBuilder.build(), Users.class);
        //第二种，测试成功
        BoolQueryBuilder bool = QueryBuilders.boolQuery();
        bool.must(QueryBuilders.idsQuery().addIds(ids.split(",")));
        bool.must(QueryBuilders.termQuery("visible", visible));
        bool.must(QueryBuilders.wildcardQuery("name", "*" + content.toLowerCase() + "*"));
        //bool.filter(QueryBuilders.rangeQuery("id").from(1).to(2).includeLower(true));
        FieldSortBuilder sort = SortBuilders.fieldSort("groupId").order(SortOrder.ASC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices("service_info")
                .withTypes("serviceinfo")
                .withPageable(PageRequest.of(0, 1000))
                .withQuery(bool)
                .withSort(sort)
                .build();
        List<Users> userLists = elasticsearchRestTemplate.queryForList(searchQuery, Users.class);
        //第三种，测试成功
        BoolQueryBuilder bools = QueryBuilders.boolQuery();
        FieldSortBuilder sorts = SortBuilders.fieldSort("time.keyword").order(SortOrder.DESC);
        SearchQuery searchQuerys = new NativeSearchQueryBuilder()
                .withIndices("service_group_info_demo")
                .withTypes("servicegroupinfodemo")
                .withPageable(PageRequest.of(0, 1000))
                .withQuery(bools)
                .withSort(sorts)
                .build();
        List<ServiceGroupInfo> userListss = elasticsearchRestTemplate.queryForList(searchQuerys, ServiceGroupInfo.class);
        //第四种，测试成功
        NativeSearchQueryBuilder queryBuilderw = new NativeSearchQueryBuilder();
        queryBuilderw.withPageable(PageRequest.of(0, 10000));
        List<ServiceGroupDictionaryInfo> userListsss = elasticsearchRestTemplate.queryForList(queryBuilderw.build(), ServiceGroupDictionaryInfo.class);
    }
}